package com.itheima.controller;

import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.data.category.DefaultCategoryDataset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/findPage")
    public List<User> findPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "10") Integer pageSize) {
        return userService.findPage(page, pageSize);
    }


    @GetMapping(value = "/downLoadXlsByJxl", name = "使用jxl下载")
    public void downLoadXlsByJxl(HttpServletResponse response) {
        //userService.downLoadByJxl(response);
    }

    @PostMapping(value = "/uploadExcel", name = "上传用户数据")
    public void uploadExcelWithException(MultipartFile file) throws Exception {
        userService.uploadExcelWithException(file);
    }

    @GetMapping(value = "/downLoadXlsxByPoi", name = "使用POI下载高版本")
    public void downLoadXlsx(HttpServletResponse response) throws Exception {
        //userService.downLoadXlsx(response);
        userService.downLoadTemplateXlsx(response);
    }

    @GetMapping(value = "/download", name = "下载某个员工的基本信息")
    public void downloadByTemplateXlsx(Long id, HttpServletResponse response) throws Exception {
        //userService.downLoadXlsx(response);
//        userService.downloadByTemplateXlsx(id,response);
//        userService.downloadByTemplateXlsxUtils(id,response);
//       userService.downloadByTemplateEasyPoiUtils(id,response);
      userService.downloadByTemplatePDF(id,response);
    }

    @GetMapping(value = "/downLoadMillion", name = "百万用户数据的导出")
    public void downLoadMillion(HttpServletResponse response) throws Exception {
        //userService.downLoadXlsx(response);
//        userService.downloadByTemplateXlsx(id,response);
        userService.downLoadMillion(response);
    }

    @GetMapping(value = "/downLoadCSV", name = "csv文件导出")
    public void downLoadCSV(HttpServletResponse response) throws Exception {
        //userService.downLoadXlsx(response);
//        userService.downloadByTemplateXlsx(id,response);
//        userService.downLoadCSV(response);
       userService.downLoadCSVEasyPOI(response);
    }

    @GetMapping("{id}")
    public User findById(@PathVariable("id") Long id) {
        return userService.findById(id);
    }


    @GetMapping(value = "/downloadContract",name = "导出用户合同")
    public void downloadContract(Long id,HttpServletResponse response) throws Exception{
//       userService.downloadContract(id,response);
        userService.downloadContractEasyPOIWord(id,response);
    }

    @GetMapping(value = "/downLoadWithEasyPOI",name = "使用EasyPOI下载Excel")
    public void downLoadWithEasyPOI(HttpServletRequest request, HttpServletResponse response) throws Exception{
        userService.downLoadXlsxWithEayPoi(request,response);
    }

    @GetMapping(value = "/downLoadPDF",name = "导出PDF")
    public void  downLoadPDF(HttpServletResponse response) throws Exception{
//        userService.downLoadPDFByDB(response);
       userService.downLoadPDFStr(response);
    }

    @GetMapping(value = "/jFreeChart",name = "返回图文报表到页面")
    public void jFreeChart(HttpServletResponse response) throws  Exception{
        //构建柱状图的数据集
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(15, "技术部", "2011");
        dataset.addValue(11, "技术部", "2012");
        dataset.addValue(10, "技术部", "2013");
        dataset.addValue(16, "技术部", "2014");

        dataset.addValue(10, "销售部", "2011");
        dataset.addValue(30, "销售部", "2012");
        dataset.addValue(6, "销售部", "2013");
        dataset.addValue(16, "销售部", "2014");

        dataset.addValue(10, "产品部", "2011");
        dataset.addValue(20, "产品部", "2012");
        dataset.addValue(30, "产品部", "2013");
        dataset.addValue(15, "产品部", "2014");

        //解决中文乱码问题
        StandardChartTheme theme = new StandardChartTheme("CN");
        //设置标题字体
        theme.setExtraLargeFont(new Font("华文宋体", Font.BOLD, 20));
        //设置图例字体
        theme.setRegularFont(new Font("华文宋体", Font.BOLD, 15));
        //设置轴向字体
        theme.setLargeFont(new Font("华文宋体", Font.BOLD, 15));
        //应用主题样式
        ChartFactory.setChartTheme(theme);

        //参数1  title 标题
        //参数2　x轴的说明
        //参数3   Y轴的说明
        //参数4   数据集
        JFreeChart jFreeChart = ChartFactory.createBarChart("各部门人数", "年度", "人数", dataset);

        //输出
        ChartUtils.writeChartAsJPEG(response.getOutputStream(), jFreeChart, 400, 300);



    }

}
